package com.yuexunit.finance.member.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.bulkstock.service.TemporaryCreditlineEvaluationService;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.EnterpriseGradeEvaluationEntity;
import com.yuexunit.finance.member.entity.TemporaryCreditlineEvaluationEntity;
import com.yuexunit.finance.member.entity.lexicon.AuditStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
*@version 创建时间：2016年9月9日 上午10:56:22
*@author wenwenlong
*类说明
*/

@Implements(TemporaryCreditlineEvaluationService.class)
public class TemporaryCreDitlineEvaluationServiceImpl implements TemporaryCreditlineEvaluationService{

	@Override
	@Transactional
	public Long createTemporaryCreDitlineEvaluation(TemporaryCreditlineEvaluationEntity entity) {
		PersistUtil.insert(entity);
		return entity.getTemporaryCreditlineEvaluationId();
	}

	@Override
	@Transactional
	public void updateTemporaryCreDitlineEvaluation(TemporaryCreditlineEvaluationEntity entity) {
		PersistUtil.update(entity);
	}

	@Override
	public TemporaryCreditlineEvaluationEntity inquireTemporaryCreDitlineEvaluation(
			Long temporaryCreditlineEvaluationId) {
		return PersistUtil.get(TemporaryCreditlineEvaluationEntity.class, temporaryCreditlineEvaluationId);
	}
	
	@Override
	public TemporaryCreditlineEvaluationEntity inquireTemporaryCreDitlineEvaluationFromEnterpriseId(Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditlineEvaluationEntity.class);
		criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		List<TemporaryCreditlineEvaluationEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(list.size() - 1);
	}

	@Override
	public PageList<TemporaryCreditlineEvaluationEntity> inquireTemporaryCreDitlineEvaluationPageList(String enterpriseName, Integer auditState, PageCriteria page) {
//		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditlineEvaluationEntity.class);
//		criteria.addOrder(Order.desc("evaluationDateTime"));
//		return PersistUtil.get(criteria, page);
		List<Long> enterpriseIds = null;
		if (StringUtils.isNotBlank(enterpriseName)) {
			DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseEntity.class);
			criteria.add(Restrictions.like("enterpriseName", "%"+enterpriseName+"%" ));
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("enterpriseId")));
			enterpriseIds = PersistUtil.get(criteria);
			if (enterpriseIds.isEmpty()) {
				return new PageList<>(page);
			}
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditlineEvaluationEntity.class);
		if (enterpriseIds != null) {
			criteria.add(Restrictions.in("enterpriseId", enterpriseIds));
		}
		if (auditState != null && auditState > 0) {
			criteria.add(Restrictions.eq("auditState", AuditStateEnum.valueOf(auditState)));
		}
//		criteria.add(Restrictions.isNull("isCreate"));
		criteria.addOrder(Order.desc("evaluationDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<TemporaryCreditlineEvaluationEntity> inquireTemporaryCreDitlineEvaluationList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditlineEvaluationEntity.class);
		return PersistUtil.get(criteria);
	}

	@Override
	public List<TemporaryCreditlineEvaluationEntity> inquireTemporaryCreDitlineEvaluationByStatePageList(
			Integer[] state) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditlineEvaluationEntity.class);
		if (state != null && state.length > 0) {
			List<AuditStateEnum> financeStateList = new ArrayList<>();
			for (Integer f : state) {
				if (f == null) {
					continue;
				}
				financeStateList.add(AuditStateEnum.valueOf(f));
			}
			if (financeStateList.size() > 0) {
				criteria.add(Restrictions.in("auditState", financeStateList.toArray(new AuditStateEnum[financeStateList.size()])));
			}
		}
		return PersistUtil.get(criteria);
	}

}
